package com.lightjet.macross.base.upms.service.impl;

import com.lightjet.macross.base.upms.domain.RoleInfo;
import com.lightjet.macross.base.upms.domain.RoleUserLink;
import com.lightjet.macross.base.upms.domain.SysUser;
import com.lightjet.macross.base.upms.domain.SystemInfo;
import com.lightjet.macross.base.upms.repository.RoleUserLinkRepository;
import com.lightjet.macross.base.upms.service.RoleUserLinkService;
import com.lightjet.macross.common.core.BaseServiceImpl;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.ConvertUtil;
import com.lightjet.macross.common.util.RedisUtil;
import com.lightjet.macross.common.util.RespUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.text.MessageFormat;
import java.util.List;

@Service
public class RoleUserLinkServiceImpl extends BaseServiceImpl<RoleUserLink,Integer> implements RoleUserLinkService {

    @Resource
    private RedisUtil redisUtil;

    //获取某个用户在某个系统中的所有角色关系
    @Override
    public List<RoleUserLink> FindByBelongsystemAndUserkey(SystemInfo systemInfo, String userkey) {
        return  ((RoleUserLinkRepository)baseRepository).findByRoleinfo_BelongsystemAndUserkey(systemInfo,userkey);
    }

    @Override
    public List<RoleInfo> FindBySystemAndUserWithAnybody(SystemInfo systemInfo, String userkey) {
        return  ((RoleUserLinkRepository)baseRepository).findBySystemAndUserWithAnybody(systemInfo,userkey);
    }

    @Override
    public List<SysUser> FindBySystemAndDeptAndRole(String ssname, Integer deptid, String rolecode, String rolename) {
        return ConvertUtil.ListMap2ListBean(((RoleUserLinkRepository)baseRepository).findBySystemAndDeptAndRole(ssname,deptid,rolecode.toUpperCase(),rolename),SysUser.class);
    }

    @Override
    public List<RoleInfo> FindBySystemShortNameAndUserKey(String systemshortname, String userkey) {
        return ((RoleUserLinkRepository)baseRepository).findBySystemShorNameAndUserKey(systemshortname,userkey);
    }

    /**
     * Description //是否已赋予用户某角色
     * @author lijia
     * @Param roleInfo 角色对象
     * @param userkey 用户对象
     * @return boolean
     * CreateTime 2020/3/7 11:32
     **/
    @Override
    public boolean HasGranted(RoleInfo roleInfo,String userkey)
    {
        return ((RoleUserLinkRepository) baseRepository).findByRoleinfo(roleInfo).stream().anyMatch(e -> userkey.equals(e.getUserkey()));
    }

    /**
     * Description //批量赋予用户某个角色
     * @author lijia
     * @Param roleInfo 角色对象
     * @param users    用户列表
     * @return com.lightjet.macross.common.dto.ResponseDTO<java.lang.String>
     * CreateTime 2020/3/7 11:21
     **/
    @Transactional
    @Override
    public ResponseDTO<String> GrantRoleToUsers(RoleInfo roleInfo, List<String> users) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            for (String user : users) {
                RoleUserLink roleUserLink = new RoleUserLink();
                roleUserLink.setRoleinfo(roleInfo);
                roleUserLink.setUserkey(user);
                this.CreateAndFlush(roleUserLink);
                //redisUtil.DeleteKeys("user::auth_userid:"+user.getId());
            }

            return RespUtil.CreateOK(stringBuilder.toString());
        }
        catch (Exception e)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespUtil.CreateError("添加用户到角色失败【已回滚】："+e.getMessage());
        }
    }

    /**
     * Description //批量撤回用户某个角色
     * @author lijia
     * @Param roleInfo 角色对象
     * @param users    用户列表
     * @return com.lightjet.macross.common.dto.ResponseDTO<java.lang.String>
     * CreateTime 2020/3/7 11:21
     **/
    @Transactional
    @Override
    public ResponseDTO<String> RevokeRoleToUsers(RoleInfo roleInfo, List<String> users) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            for (String user : users) {
                ((RoleUserLinkRepository) baseRepository).deleteByRoleAdUserkey(roleInfo,user);
                //redisUtil.DeleteKeys("user::auth_userid:"+user.getId());
            }

            return RespUtil.CreateOK(stringBuilder.toString());
        }
        catch (Exception e)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespUtil.CreateError("从角色撤销用户失败【已回滚】："+e.getMessage());
        }
    }

    /**
     * Description // 根据角色对象获取角色与用户关系集合
     * @return com.lightjet.macross.common.dto.ResponseDTO<java.lang.String>
     * CreateTime 2020/3/14 11:21
     * @author lijia
     * @Param roleInfo 角色对象
     */
    @Override
    public List<RoleUserLink> FindByRoleinfo(RoleInfo roleInfo) {
        return ((RoleUserLinkRepository) baseRepository).findByRoleinfo(roleInfo);
    }


}
